/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.osami.gateway.sensor.internal.humidity;

import java.util.logging.Logger;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Provides;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 26 mai 2010
 * Time: 18:12:40
 * To change this template use File | Settings | File Templates.
 */
@Component
@Provides
public class HumidityConverter {
    private static final Logger logger = Logger.getLogger(HumidityConverter.class.getName());

    public static final String SI_PERCENT = "SI-%";

    public static double convert(String sensorModel, int value) {
        if (sensorModel.equals(SI_PERCENT)) {
            // Store as is.
            return value;
        }
        else {
            // SHT1x family, 12-bit raw humidity measurement, no temperature compensation.
            if (sensorModel.equals("U12-SHT1x")) {
                double tmp = -2.0468 + (0.0367 * value) - (value * value * 1.5955e-6);
                // Clamp stupid values according to datasheet.
                if (tmp < 0) {
                    logger.info("Negative RH, clipping to 0%");
                    tmp = 0;
                }
                if (tmp > 99) {
                    logger.info("RH > 99%, clipping to 99%");
                    tmp = 99;
                }
                return tmp;
            }
            // SHT1x family, 8-bit raw humidity measurement, no temperature compensation.
            if (sensorModel.equals("U8-SHT1x")) {
                double tmp = (-2.0468 + (0.5872 * value) - (value * value * 4.0845e-4));
                // Clamp stupid values according to datasheet.
                if (tmp < 0) {
                    logger.info("Negative RH, clipping to 0%");
                    tmp = 0;
                }
                if (tmp > 99) {
                    logger.info("RH > 99%, clipping to 99%");
                    tmp = 99;
                }
                return tmp;
            }

            // Complain about unknown sensor models.
            logger.severe("Unknown sensor model \"" + sensorModel + "\", storing raw value as is !");
            return value;
        }
    }
}
